package com.m4f.business.service;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.m4f.business.model.Company;
import com.m4f.business.persistence.PMF;
import com.m4f.business.service.ifc.ISearchService;

public class SearchService implements ISearchService {
	
	private static final Logger LOGGER = Logger.getLogger(SearchService.class.getName());
	
	public List<Company> searchCompaniesByActivitySector(boolean validated, 
			boolean orderAsc, String activitySector) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Company.class);
		query.setFilter("validated == validatedParam " +
				"&& companyInfo.sicCode == sicCodeParam");
		query.declareParameters("java.lang.Boolean validatedParam, "+
				"java.lang.String sicCodeParam");
		String filterRoute = orderAsc?"asc":"desc";
		query.setOrdering("companyInfo.name " + filterRoute);
		List<Company> results = new ArrayList<Company>();
		try {
	        results.addAll((List<Company>)query.execute(validated, activitySector));
	    } catch(Exception e) {
	    	LOGGER.severe("Error in searchCompaniesByTag: " + 
	    			e.getMessage());
	    } finally {
	        query.closeAll();
	    }
	    return results;
	}
	
	public List<Company> searchCompaniesByInnovationType(boolean validated, 
			boolean orderAsc, String innovationType) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Company.class);
		query.setFilter("validated == validatedParam && " +
				"additionalInformation.innovationTypeCodes == innovationTypeCodeParam");
		query.declareParameters("java.lang.Boolean validatedParam, " +
				"java.lang.String innovationTypeCodeParam");
		String filterRoute = orderAsc?"asc":"desc";
		query.setOrdering("companyInfo.name " + filterRoute);
		List<Company> results = new ArrayList<Company>();
		try {
	        results.addAll((List<Company>)query.execute(validated, innovationType));
	    } catch(Exception e) {
	    	LOGGER.severe("Error in searchCompaniesByTag: " + 
	    			e.getMessage());
	    } finally {
	        query.closeAll();
	    }
	  return results;
	}
		
	public List<Company> searchCompanies(boolean validated, boolean orderAsc) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Company.class);
		query.setFilter("validated == validatedParam");
		query.declareParameters("java.lang.Boolean validatedParam");
		String filterRoute = orderAsc?"asc":"desc";
		query.setOrdering("companyInfo.name " + filterRoute);
		List<Company> results = new ArrayList<Company>();
		try {
	        results.addAll((List<Company>)query.execute(validated));
	    } catch(Exception e) {
	    	LOGGER.severe("Error in searchCompanies: " + 
	    			e.getMessage());
	    } finally {
	        query.closeAll();
	    }
	    return results;
	}
	
	
}